package com.example.john.foodstore;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

//import com.eleme.chyr.v0.Consumer;

import java.util.ArrayList;

public class RegistActivity extends Activity {

    public  final int RESULT_LOAD_IMAGE=1;
    public EditText Uname,Psw,Email,Rname;
    public ImageView image1,image2,image3;
    public EditText host_name,host_id,host_phone;
    public  Button Sub_btn,Sub_picture1,Sub_picture2,Sub_picture3;
    public String username,password,email,restaurantname;
    public String Host_name,Host_id,Host_phone;
    ArrayList<String> errors=new ArrayList<String>();
    public String picturesPath;

    //判断营业证是否合格
    /*public boolean  getRid(View view) {
        String temp = Rid.getText().toString();
        if (temp==null) {
            Log.i("hm", "商家营业证为空");
            return false;
        }
        else
            return true;
    }*/
    //判断店主电话号码信息是否正确
    public boolean getHphone(String s) {
        char[] arr = s.toCharArray();
        Log.i("hm","llllllllllllllll");
        if (11!=s.trim().length()) {
            return false;
        }
        else {
            for (int i = 0;i<11;i++) {
                if (!(arr[i]>='0'&&arr[i]<='9'))
                    return false;
            }
            return true;
        }

    }
    //判断店主身份证信息是否正确
    public  boolean getHid(String s){
        if(18!=s.trim().length()) {
            return false;
        }
        else{
            char[] chars=s.toCharArray();
            for(int i=0;i<17;i++){
                if(!(chars[i]>='0'&&chars[i]<='9'))
                    return false;
            }
            return true;
        }
    }
    //获取注册信息
    public  void fetch(View view){
        username=Uname.getText().toString();
        password=Psw.getText().toString();
        email=Email.getText().toString();
        restaurantname=Rname.getText().toString();
        Host_name=host_name.getText().toString();
        Host_id=host_id.getText().toString();
        Host_phone=host_phone.getText().toString();
    }
    //文本校验
    public boolean validate(View view){
        if((restaurantname.trim().length()==0))
            errors.add("商家信息错误");
        else if ((Host_name.trim().length()==0)||(!getHid(Host_id))||(!getHphone(Host_phone)))
            errors.add("店主信息错误");
        else if(image1.getDrawable() ==null||image2.getDrawable()==null ||image3.getDrawable()==null)
            errors.add("图片信息不能为空");
        return errors.isEmpty()?true:false;
    }

    //提交按钮事件
    public void submit(View view){
        fetch(view);
        Log.i("hm", "点击了提交按钮，，，，，，，，，，，，");
        boolean flag=validate(view);
        if(!flag){
            StringBuilder sb = new StringBuilder();
            for (String s : errors) {
                sb.append(s);
            }
            new AlertDialog.Builder(this)
                    .setTitle("提示信息")
                    .setMessage(sb.toString())
                    .setPositiveButton("确定", null)
                    .show();
            errors.clear();
        }
        else{
            Intent intent = new Intent();
            intent.setClass(this, OrderItemAdapter.class);
            startActivity(intent);
        }

    }
    //上传图片事件
    public void getPictures(View v){
        Intent intent1 = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent1, RESULT_LOAD_IMAGE);//接下来会自动调用onActivityResult方法

    }
    //取消注册事件
    public void concel(View view){}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        final Intent intent = getIntent();
        Uname = (EditText) findViewById(R.id.Uname);
        Psw =(EditText)findViewById(R.id.Psw);
        Email =(EditText)findViewById(R.id.Email);
        Rname=(EditText)findViewById(R.id.Rname);
        host_name = (EditText) findViewById(R.id.host_name);
        host_id = (EditText) findViewById(R.id.host_id);
        host_phone = (EditText) findViewById(R.id.host_phone);

        Sub_btn = (Button) findViewById(R.id.Sub_btn);
        Sub_picture1=(Button)findViewById(R.id.Sub_picture1);
        Sub_picture2=(Button)findViewById(R.id.Sub_picture2);
        Sub_picture3=(Button)findViewById(R.id.Sub_picture3);
        image1=(ImageView)findViewById(R.id.image1);
        image2=(ImageView)findViewById(R.id.image2);
        image3=(ImageView)findViewById(R.id.image3);

    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==RESULT_LOAD_IMAGE && resultCode==RESULT_OK&&data!=null) {
            Uri uri = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturesPath = cursor.getString(columnIndex);
            cursor.close();
            if(image1.getDrawable()==null)
                image1.setImageBitmap(BitmapFactory.decodeFile(picturesPath));
            else if(image2.getDrawable()==null)
                image2.setImageBitmap(BitmapFactory.decodeFile(picturesPath));
            else if(image3.getDrawable()==null)
                image3.setImageBitmap(BitmapFactory.decodeFile(picturesPath));

        }

    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_regist, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

